package com.zh.risk.service.impl;

import com.zh.risk.dao.RulerDao;
import com.zh.risk.entity.RiskField;
import com.zh.risk.entity.RiskMySQL;
import com.zh.risk.entity.RiskOperator;
import com.zh.risk.service.RulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 规则设置业务实现
 * @Author: Liard
 * @Create in: 2020/8/28 16:02
 * @Version: 1.0.1
 */
@Service
public class RulerServiceImpl implements RulerService {

    @Autowired
    private RulerDao dao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<RiskOperator> getOperatorList(RiskOperator param) {
        return dao.getOperatorList(param);
    }

    @Override
    public int addOperator(RiskOperator param) {
        return dao.addOperator(param);
    }

    @Override
    public int modOperator(RiskOperator param) {
        return dao.modOperator(param);
    }

    @Override
    public int delOperator(RiskOperator param) {
        return dao.delOperator(param);
    }

    @Override
    public List<RiskField> getFieldList(RiskField param) {
        List<RiskField> list = dao.getFieldList(param);
        List<RiskField> copyList = list.stream().filter(l -> l.getParentId().equals(0L)).collect(Collectors.toList());
        for (int i = 0; i < copyList.size(); i++) {
            List<RiskField> arrList = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                if (copyList.get(i).getId().equals(list.get(j).getParentId()))
                    arrList.add(list.get(j));
            }
            copyList.get(i).setChildren(arrList);
        }
        redisTemplate.opsForValue().set("field_Opt", copyList);
        return list;
    }

    @Override
    public int addField(RiskField param) {
        return dao.addField(param);
    }

    @Override
    public int modField(RiskField param) {
        return dao.modField(param);
    }

    @Override
    public int delField(RiskField param) {
        return dao.delField(param);
    }

    @Override
    public Integer getMerchUpdateName(RiskMySQL mySQL) {
        return dao.getMerchUpdateName(mySQL);
    }
}
